using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UMC.Data;

namespace UMC.WorkFlow
{
    /// <summary>
    /// 事件响应接口
    /// </summary>
    public interface IResponder
    {
        void OnRespond(InstanceEvent taskEvent);

    }
    /// <summary>
    /// 事件响应抽象类
    /// </summary>
    public class Responder : IResponder, IJSONType
    {
        public String Id { get; set; }
        [DesignAttribute(Caption = "描述")]
        public String Text { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="taskEvent"></param>
        protected virtual void OnRespond(InstanceEvent taskEvent)
        {

        }

        #region IResponder 成员

        void IResponder.OnRespond(InstanceEvent taskEvent)
        {
            OnRespond(taskEvent);
        }

        #endregion
        protected virtual string JSONName()
        {
            return this.GetType().Name;
        }
        public virtual Func<object> GetInstance(string prototypeName)
        {
            return () => String.Empty;
        }

        public virtual void Read(string key, object value)
        {
            switch (key)
            {
                case "Id":
                    this.Id = value as string;
                    break;
                case "Text":
                    this.Text = value as string;
                    break;
            }
        }
        protected virtual void WriteProperty(TextWriter writer)
        {
            writer.Write(',');
            JSON.Serialize("Id", this.Id, writer);
            writer.Write(',');
            JSON.Serialize("Text", this.Text, writer);
        }

        public void Write(TextWriter writer)
        {
            writer.Write('{');
            JSON.Serialize(JSON.Constructor, writer);
            writer.Write(":");
            JSON.Serialize(JSONName(), writer);
            WriteProperty(writer);
            writer.Write('}');
        }
    }

}
